package com.henry.server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.SocketException;

public class ServerThread implements Runnable{
    private ServerView sv;
    private ObjectInputStream ois;
    private ObjectOutputStream oos;
    private Socket s;

    public ServerThread() {
    }

    public ServerThread(ServerView sv, Socket s) {
        this.sv = sv;
        this.s = s;
        try {
            ois = new ObjectInputStream(s.getInputStream());
            oos = new ObjectOutputStream(s.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public ObjectInputStream getOis() {
        return ois;
    }

    public ObjectOutputStream getOos() {
        return oos;
    }

    @Override
    public void run() {
        try {
            while (true) {
                sv.serverReceive(this);
            }
        } catch (IOException | ClassNotFoundException e) {
            System.out.println(s.getInetAddress() + "断开连接");
        }finally {
            try {
                Data.closeData();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                s.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
